package t2invoke;

import java.sql.SQLException;
import java.util.List;
import java.util.Scanner;

import t2bean.BookBean;
import t2bean.CardBean;
import t2bean.LendBookRecordBean;
import t2dao.BookDao;
import t2dao.CardDao;
import t2dao.LendBookRecordDao;

public class Invoke {
	private BookDao bookdao = new BookDao();
	private CardDao carddao = new CardDao();
	private LendBookRecordDao lenddao = new LendBookRecordDao();

	public void addBook(Scanner sc) {
		System.out.println("书名");
		String bookName = sc.nextLine();
		System.out.println("书编号");
		Integer bookId = Integer.parseInt(sc.nextLine());

		BookBean findId = new BookBean();
		findId.setBookId(bookId);
		try {
			if (bookdao.select(findId).size() != 0) {
				System.out.println("编号已存在");
				return;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		findId = null;
		BookBean book = new BookBean(bookId, bookName);
		int i = bookdao.insert(book);
		if (i == 0) {
			System.out.println("添加失败");
		} else {
			System.out.println("添加成功");
		}
	}

	public void addCard(Scanner sc) {
		System.out.println("输入姓名");
		String cardName = sc.nextLine();
		System.out.println("输入卡号");
		Integer cardId = Integer.parseInt(sc.nextLine());
		CardBean findId = new CardBean();
		findId.setCardId(cardId);
		try {
			if (carddao.select(findId).size() != 0) {
				System.out.println("卡号已存在");
				return;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		findId = null;
		CardBean card = new CardBean(cardId, cardName);
		int i = carddao.insert(card);
		if (i == 0) {
			System.out.println("添加失败");
		} else {
			System.out.println("添加成功");
		}

	}

	public void lendBook(Scanner sc) {
		System.out.println("输入书名");
		String bookname = sc.nextLine();
		System.out.println("输入卡号");
		Integer cardid = Integer.parseInt(sc.nextLine());
		BookBean book = new BookBean();
		CardBean card = new CardBean();
		book.setName(bookname);
		card.setCardId(cardid);
		LendBookRecordBean lend = new LendBookRecordBean();
		try {
			List<BookBean> booklist = bookdao.select(book);
			List<CardBean> cardlist = carddao.select(card);
			if (booklist.size() == 0) {
				System.out.println("书不存在");
				return;
			}
			if (cardlist.size() == 0) {
				System.out.println("卡不存在");
				return;
			}
			if (booklist.get(0).getIsLended() == 1) {
				System.out.println("书已借出");
				return;
			}
			book = booklist.get(0);
			System.out.println(book.getLendTimes());
			card = cardlist.get(0);
			lend.setBookId(book.getBookId());
			lend.setCardId(card.getCardId());
			lenddao.insert(lend);
			book.setIsLended(1);
			book.setLendTimes(book.getLendTimes() + 1);
			System.out.println(book.getLendTimes());
			bookdao.update(book);
			System.out.println("借书成功");
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public void backBook(Scanner sc) {
		System.out.println("输入书名");
		String bookname = sc.nextLine();
		System.out.println("输入卡号");
		Integer cardid = Integer.parseInt(sc.nextLine());
		BookBean book = new BookBean();
		CardBean card = new CardBean();
		book.setName(bookname);
		card.setCardId(cardid);
		LendBookRecordBean lend = new LendBookRecordBean();

		try {
			List<BookBean> booklist = bookdao.select(book);
			List<CardBean> cardlist = carddao.select(card);
			if (booklist.size() == 0) {
				System.out.println("书不存在");
				return;
			}
			if (cardlist.size() == 0) {
				System.out.println("卡不存在");
				return;
			}
			if (booklist.get(0).getIsLended() != 1) {
				System.out.println("书未借出");
				return;
			}
			book = booklist.get(0);
			card = cardlist.get(0);
			lend.setBookId(book.getBookId());
			lend.setCardId(card.getCardId());

			List<LendBookRecordBean> lendList = lenddao.select(lend);
			if (lendList.size() == 0) {
				System.out.println("卡号或书名错误");
				return;
			}
			System.out.println("还书成功");
			book.setIsLended(0);
			bookdao.update(book);

		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public void showBook() throws SQLException {
		List<BookBean> list = bookdao.select(null);
		sort(list);
		for (BookBean book1 : list) {
			System.out.println("编号：" + book1.getBookId());
			System.out.println("名字：" + book1.getName());
			System.out.println("借阅次数：" + book1.getLendTimes());
			if (book1.getIsLended() == 1) {
				System.out.println("已借出");
			} else {
				System.out.println("未借出");
			}
			System.out.println();
		}
	}

	void sort(List<BookBean> list) {
		BookBean targer;
		for (int i = 1; i < list.size(); ++i) {
			for (int j = 0; j < i; ++j) {
				if (list.get(i).getLendTimes().compareTo(list.get(j).getLendTimes()) > 0) {
					targer = list.get(i);
					for (int k = i; k >= j + 1; --k) {
						list.set(k, list.get(k - 1));
					}
					list.set(j, targer);
				}
			}
		}
	}

	public void showCardLendRecord(Scanner sc) throws SQLException {
		System.out.println("输入卡号");
		Integer cardid = Integer.parseInt(sc.nextLine());
		LendBookRecordBean lend = new LendBookRecordBean();
		lend.setCardId(cardid);
		List<LendBookRecordBean> list = lenddao.select(lend);
		System.out.println("卡号：" + cardid);
//		System.out.println(list.get(1).getBookId());
		int o = 0;
		for (LendBookRecordBean lend1 : list) {
			BookBean book = new BookBean();
			book.setBookId(lend1.getBookId());
			System.out.println(o++);
			List<BookBean> list2 = bookdao.select(book);
			System.out.println("书编号：" + list2.get(0).getBookId());
			System.out.println("书名：" + list2.get(0).getName());
			System.out.println();
		}
	}

	public void showBookLendRecord(Scanner sc) throws SQLException {
		System.out.println("输入书名");
		String bookname = sc.nextLine();
		LendBookRecordBean lend = new LendBookRecordBean();
		BookBean book = new BookBean();
		book.setName(bookname);
		book = bookdao.select(book).get(0);
		lend.setBookId(book.getBookId());
		List<LendBookRecordBean> list = lenddao.select(lend);
		System.out.println("书名：" + bookname);
		for (LendBookRecordBean lend1 : list) {
			BookBean book1 = new BookBean();
			book1.setBookId(lend1.getBookId());
			List<BookBean> list2 = bookdao.select(book1);
			System.out.println("编号：" + list2.get(0).getBookId());
			System.out.println("书名：" + list2.get(0).getName());
			System.out.println();
		}
	}
}
